/**
 * @file      ac_tlm_card.h
 * @author    Bruno de Carvalho Albertini
 * 
 * @author    The ArchC Team
 *            http://www.archc.org/
 *
 *            Computer Systems Laboratory (LSC)
 *            IC-UNICAMP
 *            http://www.lsc.ic.unicamp.br/
 * 
 * @version   0.1
 * @date      Sun, 02 Apr 2006 08:07:46 -0200
 * 
 * @brief     Defines a ac_tlm card.
 * 
 * @attention Copyright (C) 2002-2005 --- The ArchC Team
 * 
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 * 
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 * 
 * 
 */

//////////////////////////////////////////////////////////////////////////////

#ifndef AC_TLM_CARD_H_
#define AC_TLM_CARD_H_

//////////////////////////////////////////////////////////////////////////////

// Standard includes
#include <stdio.h>
// SystemC includes
#include <systemc>
// ArchC includes
#include "ac_tlm_protocol.H"

/*
#define LST_ELEITORES  "../../ip/ac_tlm_card/lists/eleitores.txt"
#define LST_CANDIDATOS "../../ip/ac_tlm_card/lists/candidatos.txt"
#define LST_PARTIDOS   "../../ip/ac_tlm_card/lists/partidos.txt"
#define LST_MESARIOS   "../../ip/ac_tlm_card/lists/mesarios.txt"
*/

#define ARQ_ELEITORES  "../../ip/ac_tlm_card/files/eleitores"
#define ARQ_CANDIDATOS "../../ip/ac_tlm_card/files/candidatos"
#define ARQ_PARTIDOS   "../../ip/ac_tlm_card/files/partidos"
#define ARQ_MESARIOS   "../../ip/ac_tlm_card/files/mesarios"

#define END_ELEITORES  0x000000
#define END_CANDIDATOS 0xE00000
#define END_PARTIDOS   0xF00000
#define END_MESARIOS   0xF80000
#define END_FIM        0x1000000

//////////////////////////////////////////////////////////////////////////////

// using statements
using tlm::tlm_transport_if;

//////////////////////////////////////////////////////////////////////////////

/// Namespace to isolate card from ArchC
namespace user
{

/// A TLM card
class ac_tlm_card :
  public sc_module,
  public ac_tlm_transport_if // Using ArchC TLM protocol
{
public:

  /// Exposed port with ArchC interface
  sc_export< ac_tlm_transport_if > target_export;
  /// Internal write
  ac_tlm_rsp_status writec( const uint32_t & , const uint32_t & );
  /// Internal read
  ac_tlm_rsp_status readc( const uint32_t & , uint32_t & );
  
  /**
   * Implementation of TLM transport method that
   * handle packets of the protocol doing apropriate actions.
   * This method must be implemented (required by SystemC TLM).
   * @param request is a received request packet
   * @return A response packet to be send
  */
  ac_tlm_rsp transport( const ac_tlm_req &request ) {

    ac_tlm_rsp response;

    switch( request.type ) {
    case READ :     // Packet is a READ one
      #ifdef DEBUG  // Turn it on to print transport level messages
	cout << "Transport WRITE in the CARD at " << request.addr << " value ";
	cout << response.data << endl;
      #endif
      response.status = readc( request.addr , response.data );
      break; 
    case WRITE:     // Packet is a WRITE
      #ifdef DEBUG
	cout << "Transport READ in the CARD at " << request.addr << " value ";
	cout << request.data << endl;
      #endif
      response.status = writec( request.addr , request.data );
      break;
    default :
      response.status = ERROR;
      break; 
    }

    return response;
  }


  /** 
   * Default constructor.
   * 
   */
  ac_tlm_card( sc_module_name module_name );

  /** 
   * Default destructor.
   */
  ~ac_tlm_card();

private:
  FILE *eleitores, *candidatos, *partidos, *mesarios;

};

};

#endif //AC_TLM_CARD_H_
